var express = require("express");
const { get } = require("mongoose");
var router = express.Router();
var { couponsModel, userCouponsModel } = require("../model/couponsmodel");
/* GET home page. */
router.get("/find", async function (req, res, next) {
  let data = await couponsModel.find().populate("good");
  // console.log(data);
  res.send({
    code: 200,
    data,
  });
});
router.post("/good/:id", async function (req, res, next) {
  let id = req.params.id;
  // console.log(id, "idddddddddddd");
  let data = [];
  if (id == 1) {
    data = await couponsModel.find().populate("good");
  } else {
    data = await couponsModel.find({ classid: id }).populate("good");
  }
  // console.log(data);
  res.send({
    code: 200,
    data,
  });
});
router.post("/done/:id", async function (req, res, next) {
    let id = req.params.id;
    // console.log(id);
let  arr = await couponsModel.find({_id:id}).populate("good");
  let data = await couponsModel.updateOne({_id:id},{$set:{done:!arr[0].done}}).populate("good");
  res.send({
    code: 200,
    data,
  });
});
module.exports = router;
